package com.dobby.pea.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dobby.pea.common.BaseContext;
import com.dobby.pea.common.R;
import com.dobby.pea.dto.ShoppingCartDto;
import com.dobby.pea.entity.tbShoppingCart;
import com.dobby.pea.entity.tbUser;
import com.dobby.pea.service.ShoppingCartService;
import com.dobby.pea.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Dobby
 * @version 1.0
 */

@RestController
@RequestMapping("/api/shoppingcart")
@Slf4j
public class ShoppingCartController {


    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 获取所有购物车
     * @return
     */
    @GetMapping("/all")
    public R<List<tbShoppingCart>> allShoppingCart(){

        LambdaQueryWrapper<tbShoppingCart> queryWrapper = new LambdaQueryWrapper<>();

        List<tbShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);

    }

    /**
     * 添加购物车 前端给number字段，直接录入
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<tbShoppingCart> add(@RequestBody tbShoppingCart shoppingCart){

        //设置用户id，指定当前是哪个用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setCreateTime(LocalDateTime.now());

        log.info("购物车数据:{}",shoppingCart);

        LambdaQueryWrapper<tbShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(tbShoppingCart::getUserId,shoppingCart.getUserId());
        queryWrapper.eq(tbShoppingCart::getGoodId,shoppingCart.getGoodId());

        int count = shoppingCartService.count(queryWrapper);

        if (count<=0) shoppingCartService.save(shoppingCart);

        shoppingCartService.update(shoppingCart, queryWrapper);


        return R.success(shoppingCart);
    }



    /**
     * 分页查询 查看购物车 为实现分页查询
     * @return
     */
    @GetMapping("/list")
    public R<Page> list(int page, int pageSize){
        log.info("查看购物车...");

        return shoppingCartService.ShoppingCartDtoPage(page,pageSize,BaseContext.getCurrentId());
    }

    /**
     * 清空购物车 根据用户
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){


        LambdaQueryWrapper<tbShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(tbShoppingCart::getUserId,BaseContext.getCurrentId());

        int count = shoppingCartService.count(queryWrapper);

        if (count<=0){
            return R.error("购物车为空");
        }else {
            shoppingCartService.remove(queryWrapper);
            return R.success("清空购物车成功");
        }


    }


    /**
     * 批量删除购物车内
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestBody List<Long> ids){
        log.info("ids:{}",ids);

        shoppingCartService.removeByIds(ids);

        return R.success("删除成功");
    }

}
